<?php

defined('BASEPATH') or die('No direct script access allowed');

/**
 * 日记控制器
 * @author  Karson
 * @name    diary.php
 * @since   2012-12-2 23:37:51
 */
class Diary_Controller extends Controller {
    
    function _init() {
        check_level();
    }
    
    function index($family = 0, $category_id = 0, $page = 1, $pagesize = 10) {
        if ($category_id > 0)
            $where[] = "category_id='{$category_id}'";
        if ($family) {
            $family_list = $this->family->get_family_list();
            if ($this->auth->fields['family_id']) {
                $family_ids = implode(',', array_keys($family_list));
                $where[] = "user_id='{$this->auth->user_id}' OR (user_id IN ({$family_ids}) AND family='1')";
            } else {
                $where[] = "user_id='{$this->auth->user_id}'";
            }
            $this->view->assign("family_list", $family_list);
        } else {
            $where[] = "user_id='{$this->auth->user_id}'";
        }
        $where = implode(' AND ', $where);
        $result = $this->db->one("SELECT COUNT(*) AS num FROM {pre}diary cp WHERE {$where}");
        $total = $result['num'];
        $offset = ($page - 1) * $pagesize;
        $pagelist = $this->page->get("/diary/index/{$family}/{$category_id}/{page}/{$pagesize}/", $total, $page, $pagesize, false, false);

        $list = array();
        $this->db->query("SELECT * FROM {pre}diary WHERE {$where} ORDER BY createtime DESC LIMIT {$offset},{$pagesize}", "list");
        while ($row = $this->db->get_array("list")) {
            $list[] = $row;
        }
        $data = array("pagelist" => $pagelist, 'list' => $list, 'category_id' => $category_id, 'family' => $family);
        $this->view->assign($data);
        render("diary/index");
    }

    function add() {
        if ($this->form->is_submit()) {
            $notinfamily = $this->input->post('notinfamily');
            $star = $this->input->post('star');
            $value = array(
                'uuid' => get_uuid(),
                'user_id' => $this->auth->user_id,
                'category_id' => 0,
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content'),
                'createtime' => TIME,
                'star' => $star ? 1 : 0,
                'family' => $notinfamily ? 0 : 1,
                'state' => 1
            );
            $id = $this->db->insert("{pre}diary", $value);
            $result = array('info' => '提交成功！', 'status' => 'y', 'data' => array());
            echo json_encode($result);
            exit;
        }
        render("diary/add");
    }

    function edit($id = 0) {
        $show = $this->db->one("SELECT * FROM {pre}diary WHERE id='{$id}' AND user_id='{$this->auth->user_id}'");
        if ($this->form->is_submit()) {
            $notinfamily = $this->input->post('notinfamily');
            $star = $this->input->post('star');
            $value = array(
                'category_id' => $this->input->post('category_id'),
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content'),
                'star' => $star ? 1 : 0,
                'family' => $notinfamily ? 0 : 1,
                'modifytime' => TIME
            );
            $id = $this->db->update("{pre}diary", $value, array('id' => $id));
            $result = array('info' => '提交成功！', 'status' => 'y', 'data' => array());
            echo json_encode($result);
            exit;
        }
        $this->view->assign("show", $show);
        render("diary/edit");
    }

    function show($id = 0) {
        $show = $this->db->one("SELECT ud.*,u.nickname,u.face FROM {pre}diary ud LEFT JOIN {pre}user u ON ud.user_id=u.id WHERE ud.id='{$id}'");
        if ($show && check_in_family($show['user_id'])) {
            $show['user'] = $this->auth->get_user_data($show['user_id']);
            $show['isauthor'] = $show['user_id'] == $this->auth->user_id ? 1 : 0;
            $this->view->assign("show", $show);
            render("diary/show");
        } else {
            render("error", '参数不正确或授权错误');
        }
    }

    function star($id = 0) {
        $show = $this->db->one("SELECT ud.*,u.nickname,u.face FROM {pre}diary ud LEFT JOIN {pre}user u ON ud.user_id=u.id WHERE ud.id='{$id}' AND user_id='{$this->auth->user_id}'");
        if ($show) {
            $outstate = $show['star'] ? 0 : 1;
            $this->db->update("{pre}diary", array('star' => $outstate, 'modifytime' => TIME), array('id' => $id));
            json_output(0, $outstate);
        } else {
            json_output(-1, '参数不正确');
        }
    }

    function del($id = 0) {
        $result = $this->db->delete("{pre}diary", array("id" => $id, "user_id" => $this->auth->user_id));
        if ($result) {
            json_output(0);
        } else {
            json_output(-1);
        }
    }

}